package com.cari.oa.service;

import java.util.ArrayList;
import java.util.Iterator;
import java.util.List;

import javax.annotation.Resource;

import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import com.cari.oa.dao.BaseDao;
import com.cari.oa.dao.SysDeptDao;
import com.cari.oa.domain.SysDeptEntity;
import com.cari.oa.util.StrUtils;

/**
 * 
* Title: SysDeptService
* Description: 部门service
* Company: cari 
* @author    hjx 
* @date       2013-1-24
 */
@Service
@Transactional(rollbackFor = Exception.class)
public class SysDeptService extends BaseService<SysDeptEntity> {
	
	@Resource
	private SysDeptDao sysDeptDao;

	@Override
	public BaseDao<SysDeptEntity> getDao() {
		return sysDeptDao;
	}
    /**
    * Title: getPage
    * Description:分页获取风险列表
    * @author    hjx 
    * @date       2013-1-21 
    * @param riskType
    * @param status
    * @param projectId
    * @param page
    * @param size
    * @return
    */
	@Transactional(readOnly = true)
    public List<SysDeptEntity> getPage(String deptNo, String deptName,String pNo, int page, int size) {
    	return sysDeptDao.getPage(deptNo, deptName,pNo, page, size);
    }
    
   /**
   * Title: getCount
   * Description:查询的记录数
   * @author    hjx 
   * @date       2013-1-21 
   * @param riskType
   * @param status
   * @param projectId
   * @return
   */
	@Transactional(readOnly = true)
    public long getCount(String deptNo, String deptName,String pNo) {
    	return sysDeptDao.getCount(deptNo, deptName,pNo);
    }	
	
    /**
    * Title: existByDeptNo
    * Description:检查部门编号是否存在
    * @author    hjx 
    * @date       2013-1-22 
    * @param deptNo
    * @return
    */
    @Transactional(readOnly = true)
	public boolean existByDeptNo(String deptNo) {
    	return sysDeptDao.existByDeptNo(deptNo);
	}
    
    /**
    * Title: existByDeptName
    * Description:检查部门名称是否存在
    * @author    hjx 
    * @date       2013-1-22 
    * @param deptName
    * @return
     */
    @Transactional(readOnly = true)
	public boolean existByDeptName(String deptName) {
    	return sysDeptDao.existByDeptName(deptName);
	}
    
    /**
	* Title: getByDeptNo
	* Description:根据部门编号获取部门
	* @author    hjx 
	* @date       2013-1-22 
	* @param deptNo
	* @return
	*/
    @Transactional(readOnly = true)
	public SysDeptEntity getByDeptNo(String deptNo) {
		return sysDeptDao.getByDeptNo(deptNo);
	}
    
    /**
     * Title: getChildDept
     * Description: 获取某个部门底下的子部门
     * @author    hjx 
     * @date       2013-1-22 
     * @param deptNo
     * @return
     */
	public List<SysDeptEntity> getChildDept(String deptNo) {
		
		return sysDeptDao.getChildDept(deptNo);
	}
		
	/**
	* Title: getTree
	* Description: 根据节点获得树
	* @author    hjx 
	* @date       2013-1-24 
	* @param isAll  等于 true 表示全部节点的树，false 表示去除叶子节点的树
	* @return
	*/
    @Transactional(readOnly = true)
	public List<String> getTree(boolean isAll) {
    	List<String> levString = new ArrayList<String>();
    	List<SysDeptEntity> treeNodes = new ArrayList<SysDeptEntity>();
    	if(isAll)
    		treeNodes = getTree(sysDeptDao.getAll()); 
    	else
    		treeNodes = getTree(sysDeptDao.getPage(null, null, null, 0, 0)); 
		for(SysDeptEntity currentNode : treeNodes)
		{
			levString.add(currentNode.toTreeString());
		}
		return levString;
	}
    
	/**
	* Title: getTree
	* Description:获取树型列表
	* @author    hjx 
	* @date       2013-1-23 
	* @param src
	* @return
	*/
	public  List<SysDeptEntity> getTree(List<SysDeptEntity> src)
	{
		List<SysDeptEntity> parents = new ArrayList<SysDeptEntity>();  //存储一级节点
		List<SysDeptEntity> last = new ArrayList<SysDeptEntity>();     //存储非一级节点
		for (SysDeptEntity ent : src)
		{
			if (StrUtils.isEmpty(ent.getpNo()))                        //一级节点的PNO为null
			{
				SysDeptEntity result = ent;
				result.setChildDept(new ArrayList<SysDeptEntity>());
				parents.add(result);
			}else
			{
				last.add(ent);
			}
		}
		buildTree(parents, last);
		return parents;
	}
	
	/**
	* Title: buildTree
	* Description:递归构建树
	* @author    hjx 
	* @date       2013-1-23 
	* @param parents 一级节点（pNo不等于null）
	* @param childList 非一级节点（中间节点和子节点）
	*/
	private  void buildTree(List<SysDeptEntity> parents, List<SysDeptEntity> childList)
	{
		List<SysDeptEntity> record = new ArrayList<SysDeptEntity>();
		for (Iterator<SysDeptEntity> parentIt = parents.iterator(); parentIt.hasNext();)
		{
			SysDeptEntity currentParent = parentIt.next();
			if (currentParent.getDeptNo() != null)
			{
				for (Iterator<SysDeptEntity> childIt = childList.iterator(); childIt.hasNext();)
				{

					SysDeptEntity currentChild = childIt.next();
					if (currentParent.getDeptNo().equals(currentChild.getpNo()))
					{
						if (null == currentParent.getChildDept())
						{
					    	currentParent.setChildDept(new ArrayList<SysDeptEntity>());
						}
						currentParent.getChildDept().add(currentChild);
						record.add(currentChild);
						childIt.remove();
					}
				}
			}
		}
		if (childList.size() == 0)
		{
			return;
		}
		else
			buildTree(record, childList);
	}
}
